<template>
  <div>
    <el-dialog
      title="派单"
      width="800px"
      class="icon-dialog"
      :visible.sync="show"
      :before-close="closeForm"
      append-to-body
      @open="open"
    >
      <el-form
        ref="form"
        :size="size"
        :model="form"
        :label-width="$store.getters.device !== 'mobile' ? '18%' : '90px'"
      >
        <table
          class="table table-bordered"
          align="center"
          width="100%"
          style="word-break: break-all; margin-bottom: 50px; font-size: 13px"
        >
          <tbody>
            <tr>
              <td class="title" width="100">订单编号：</td>
              <td>
                {{ form.orderInfo.order_num_alias }}
              </td>
            </tr>
            <tr>
              <td class="title" width="100">下单用户：</td>
              <td>
                {{ form.orderInfo.member.nickname }}
              </td>
            </tr>
            <tr>
              <td class="title" width="100">下单时间：</td>
              <td>
                {{ form.orderInfo.create_time }}
              </td>
            </tr>
            <tr>
              <td class="title" width="100">预约时间：</td>
              <td>
                {{ form.orderInfo.ServiceTime }}
              </td>
            </tr>
            <tr>
              <td class="title" width="100">订单状态：</td>
              <td>
                {{ form.orderInfo.orderStatus.name_yuyue }}
              </td>
            </tr>
            <tr>
              <td class="title" width="100">支付方式：</td>
              <td>
                {{ form.orderInfo.paymethod.title }}
              </td>
            </tr>
            <tr>
              <td class="title" width="100">支付时间：</td>
              <td>
                {{ form.orderInfo.pay_time }}
              </td>
            </tr>
            <tr>
              <td class="title" width="100">订单金额：</td>
              <td>
                {{ form.orderInfo.total }}
              </td>
            </tr>
            <tr>
              <td class="title" width="100">服务地址：</td>
              <td>
                <div>
                  联系人：{{ form.orderInfo.shipping_name }}
                  {{ form.orderInfo.shipping_tel }}
                </div>
                <div>
                  地 址：{{ form.orderInfo.shipping_province
                  }}{{ form.orderInfo.shipping_city
                  }}{{ form.orderInfo.shipping_district
                  }}{{ form.orderInfo.shipping_address }}
                </div>
              </td>
            </tr>
            <tr>
              <td class="title" width="100">订购的服务：</td>
              <td>
                <div v-for="(item, index) in form.goods" :key="index">
                  <div class="image__preview">
                    <el-image
                      v-if="item.image"
                      class="table_list_pic"
                      :src="item.image"
                      :preview-src-list="[item.image]"
                    />
                  </div>
                  <div class="goodstext">
                    <div>{{ item.name + "\n" }}</div>
                    <div>数量：{{ item.quantity }}</div>
                    <div>小计：{{ item.total }}</div>
                  </div>
                </div>
              </td>
            </tr>
          </tbody>
        </table>
        <el-row v-if="!form.sid">
          <el-col :span="24">
            <el-form-item label="指派给">
              <el-radio-group
                v-model="form.orderInfo.sendto"
                style="padding-left: 10px"
              >
                <el-radio :label="1">商家/门店</el-radio>
                <el-radio :label="2">师傅</el-radio>
              </el-radio-group>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row v-if="!form.sid && form.orderInfo.sendto == 1">
          <el-col :span="24">
            <el-form-item label="商家">
              <el-select
                v-model="form.orderInfo.sid"
                style="width: 100%"
                filterable
                clearable
                placeholder="请选择"
                @change="changeOnMainPersonBB"
              >
                <el-option
                  v-for="(item, i) in sidarray"
                  :key="i"
                  :label="item.key"
                  :value="item.val"
                />
              </el-select>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row v-if="!form.sid && form.orderInfo.sendto == 2">
          <el-col :span="24">
            <el-form-item label="">
              <!-- <el-select v-model="form.uuid" multiple style="width:100%" filterable clearable placeholder="请选择">
                <el-option v-for="(item,i) in technical" :key="i" :label="item.key" :value="item.val" />
              </el-select> -->
              <el-table
                :data="form.workerList"
                border
                style="width: 100%"
              >
                <el-table-column fixed prop="date" label="姓名" min-width="150">
                  <template slot-scope="scope">
                    <el-select
                      v-model="scope.row.uuid"
                      style="width: 100%"
                      filterable
                      clearable
                      placeholder="请选择"
                      @change="staffChange"
                    >
                      <el-option
                        v-for="(item, i) in technical"
                        :key="i"
                        :label="item.key"
                        :value="item.val"
                      />
                    </el-select>
                  </template>
                </el-table-column>
                <el-table-column prop="name" label="佣金" min-width="120">
                  <template slot-scope="scope">
                    <el-input
                      v-model="scope.row.money"
                      placeholder="请输入佣金"
                    ></el-input>
                  </template>
                </el-table-column>
                <el-table-column prop="province" label="备注" width="200">
                  <template slot-scope="scope">
                    <el-input
                      v-model="scope.row.memo"
                      placeholder="备注"
                      maxlength="20"
                    ></el-input>
                  </template>
                </el-table-column>
                <el-table-column label="操作" width="80">
                  <template slot-scope="scope">
                    <el-button
                      @click.native.prevent="deleteWorker(scope.$index)"
                      type="text"
                      size="small"
                    >
                      移除
                    </el-button>
                  </template>
                </el-table-column>
              </el-table>
              <div style="margin-top: 20px">
                <el-button @click="addWorker">添加师傅</el-button>
              </div>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row v-if="form.sid">
          <el-col :span="24">
            <el-form-item label="派单给师傅">
              <el-select
                v-model="form.siduuid"
                style="width: 100%"
                filterable
                clearable
                placeholder="请选择"
              >
                <el-option
                  v-for="(item, i) in technical"
                  :key="i"
                  :label="item.key"
                  :value="item.val"
                />
              </el-select>
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button :loading="loading" type="primary" @click="submit">
          <span v-if="!loading">确 定</span>
          <span v-else>提 交 中...</span>
        </el-button>
        <el-button @click="closeForm">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>
<script>

function hasDuplicate(arr) {
  for (let i = 0; i < arr.length; i++) {
    for (let j = i+1; j < arr.length; j++) {
      if (arr[i].uuid === arr[j].uuid) {
        return true;
      }
    }
  }
  return false;
}
export default {
  name: "Orderdetail",
  components: {},
  props: {
    show: {
      type: Boolean,
      default: true,
    },
    size: {
      type: String,
      default: "mini",
    },
    info: {
      type: Object,
    },
  },
  data() {
    return {
      form: {
        orderInfo: {
          orderStatus: {},
          address: {},
          paymethod: {},
          member: {},
          workerList:[]
        },
      },
      loading: false,
      sidarray: {},
      technical: {},
    };
  },
  watch: {
    show(val) {},
  },
  methods: {
    open() {
      this.form = this.info;
      this.sidarray = this.form.sidarray;
      this.technical = this.form.technical;
    },
    changeOnMainPersonBB(sid) {
      this.$api.post("/order/getTechnical", { sid: sid }).then((res) => {
        this.technical = res.data.technical;
      });
    },
    submit() {
      if(hasDuplicate(this.form.workerList)){
      return  this.$message.warning('不能重复选择服务师傅');
      }
      this.$refs["form"].validate((valid) => {
        if (valid) {
          this.loading = true;
          this.$api
            .post("/order/staff", this.form)
            .then((res) => {
              this.$message({
                message: res.msg,
                type: "success",
              });
              this.$emit("refesh_list");
              this.closeForm();
            })
            .catch(() => {
              this.loading = false;
            });
        }
      });
    },
    addWorker() {
      this.form.workerList.push({ uuid: "", money: "", memo: "" });
    },
    staffChange(e){
      // if(hasDuplicate(this.form.workerList)){
      //   this.$message.warning('选择的师傅有重复');
      // }
    },
    deleteWorker(index) {
      this.$delete(this.form.workerList, index);
    },
    closeForm() {
      this.$emit("update:show", false);
      this.loading = false;
      if (this.$refs["form"] !== undefined) {
        this.$refs["form"].resetFields();
      }
    },
  },
};
</script>
<style>
.image__preview {
  float: left;
}

.goodstext {
  float: left;
  padding-left: 10px;
  width: 200px;
}

.table_list_pic {
  width: 60px;
}
</style>
